package service;



import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import dao.JobInfoDao;
import entity.JobInfo;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import utils.UrlUtils;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

public class ReadJobInfoService {
    private JobInfoDao jobInfoDao = new JobInfoDao();
    public  void readJobData(String cityCode,String keyWord,int pageNum) throws IOException, SQLException, ClassNotFoundException {
        String url = UrlUtils.generateUrl(cityCode,keyWord,pageNum);

        Document document = getDocument(url);

        Elements allJob = document.select(".job-primary");
        if(allJob == null|| allJob.size() == 0){
            return;
        }
        List<JobInfo> jobInfoList = new ArrayList<>();
        for(Element thisJob:allJob){

            Element jobBoxElement = thisJob.selectFirst(".primary-box");
            String jobId = jobBoxElement.attr("data-jobid");
            Element jobNameElement = thisJob.selectFirst(".job-name>a");
            Element jobAreaElement = thisJob.selectFirst(".job-area");
            Element jobCompanyElement = thisJob.selectFirst(".company-text>.name>a");
            Element jobCompanyDesc = thisJob.selectFirst(".company-text>p");
            String[] descArr = jobCompanyDesc.text().split(" ");
            Element jobSalaryElement = thisJob.selectFirst(".job-limit>span");
            Element jobSalaryLmit = thisJob.selectFirst(".job-limit>p");
  /*          String jobId, String jobName,
                    String jobArea, String jobCompany,
                    String companyDesc1, String companyDesc2,
                    String companyDesc3, String jobSalary,
                    String jobYear, String jobXuli*/
            String[] limitArr = jobSalaryLmit.text().split(" ");
            JobInfo jobInfo = new JobInfo(jobId,jobNameElement.text(),jobAreaElement.text(),
                    jobCompanyElement.text(),null,null,null,
                    jobSalaryElement.text(),null,null,keyWord,cityCode);
            jobInfoList.add(jobInfo);
           // System.out.println(jobNameElement.text()+"-->"+jobAreaElement.text()+"-->"+jobCompanyElement.text()+"-->"+jobSalaryElement.text());
        }

        jobInfoDao.batchInsert(jobInfoList);
        pageNum++;
        readJobData(cityCode,keyWord,pageNum);
    }

    public Document getDocument(String url) throws IOException {
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        //屏蔽日志信息
        LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log",
                "org.apache.commons.logging.impl.NoOpLog");
        java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
        //支持JavaScript
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setActiveXNative(false);
        // webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setTimeout(5000);

        System.out.println(url);
        HtmlPage rootPage = webClient.getPage(url);
        //设置一个运行JavaScript的时间
        webClient.waitForBackgroundJavaScript(5000);
        String html = rootPage.asXml();

        //
        Document document = Jsoup.parse(html);
        return  document;
    }
}
